package com.hlkj.warboot.configurer.annotation;

import com.hlkj.warboot.module_sys.entity.AdminEntity;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @Description 登录用户对象参数解析器
 * @Author lixing
 * @Date 2021/6/11 11:38
 */
public class LoginUserInfoMethodArgumentResolver implements HandlerMethodArgumentResolver {

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.hasParameterAnnotation(LoginUserInfoAnnotation.class); // 支持带@LoginUserInfoAnnotation注解的参数
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		AdminEntity loginUserInfo = (AdminEntity) webRequest.getAttribute("loginUserInfo", NativeWebRequest.SCOPE_REQUEST);
		return loginUserInfo;
	}

}
